package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.inv.InvAccountTypeDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvAccountType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_INVACCOUNTTYPE)
public class InvAccountTypeController extends SupporterController{
	private InvAccountTypeDAO iatDAO;
	private OrganDAO organDAO;
	
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.INVACCOUNTTYPE_INIT);
	}
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ON, Parm.CN, Parm.TIME1, Parm.TIME2, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (iatDAO.findByMap(oc, map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.INVACCOUNTTYPE_LIST);
	}
	
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if (null != code) {
			InvAccountType iat = iatDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INVACCOUNTTYPE, iat);
		}
		return new ModelAndView(Url.INVACCOUNTTYPE_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvAccountType ia, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (ia == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (ia.getCode() == null) {
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<InvAccountType> iac = iatDAO.findByNameOc(ia.getName(), oc);
			if (iac != null && iac.size() > 0)  {
				print(response, Msg.EXISTS);
				return;
			}
			ia.setOc(oc);
			ia.setCu(cu_code(request));
			ia.setCt(new Date());
			ia.setDeleted(false);
			result = iatDAO.add(ia);
		} else {
			result = iatDAO.update(ia);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvAccountType iat = iatDAO.get(Integer.parseInt(code));
		if (null == iat) {
			print(response, Msg.NOT_FIND);
			return;
		}
		iat.setDeleted(true);
		if (iatDAO.upd(iat) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvAccountType iat = iatDAO.get(Integer.parseInt(code));
		if (null == iat) {
			print(response, Msg.NOT_FIND);
			return;
		}
		iat.setDeleted(false);
		if (iatDAO.upd(iat) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	
	@Autowired
	public void setIatDAO(InvAccountTypeDAO iatDAO) {
		this.iatDAO = iatDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
